package com.yida.gwj.finance.service.impl;

import com.yida.common.core.enums.UserStatus;
import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.StringUtils;
import com.yida.gwj.api.domain.GwjFinanceMoney;
import com.yida.gwj.common.constant.GwjConstants;
import com.yida.gwj.finance.mapper.GwjFinanceMoneyMapper;
import com.yida.gwj.finance.service.IGwjFinanceMoneyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 缴存金额收入总帐明细Service业务层处理
 *
 * @author lqq
 * @date 2022-12-28
 */
@Service
public class GwjFinanceMoneyServiceImpl implements IGwjFinanceMoneyService {
	@Resource
	private GwjFinanceMoneyMapper financeMoneyMapper;

	/**
	 * 查询缴存金额收入总帐明细
	 *
	 * @param moneyId 缴存金额收入总帐明细主键
	 * @return 缴存金额收入总帐明细
	 */
	@Override
	public GwjFinanceMoney selectFinanceMoneyByMoneyId(Long moneyId) {
		return financeMoneyMapper.selectFinanceMoneyByMoneyId(moneyId);
	}

	/**
	 * 查询缴存金额收入总帐明细列表
	 *
	 * @param financeMoney 缴存金额收入总帐明细
	 * @return 缴存金额收入总帐明细
	 */
	@Override
	public List<GwjFinanceMoney> selectFinanceMoneyList(GwjFinanceMoney financeMoney) {
		return financeMoneyMapper.selectFinanceMoneyList(financeMoney);
	}

	/**
	 * 查询缴存金额收入总帐明细列表（详情含业务信息）
	 *
	 * @param financeMoney 缴存金额收入总帐明细
	 * @return 缴存金额收入总帐明细集合
	 */
	@Override
	public List<GwjFinanceMoney> selectFinanceMoneyDetailList(GwjFinanceMoney financeMoney) {
		return financeMoneyMapper.selectFinanceMoneyDetailList(financeMoney);
	}

	/**
	 * 统计缴存金额
	 *
	 * @param bankId 银行ID
	 * @param areaId 地区ID
	 * @return 缴存金额收入总帐明细集合
	 */
	@Override
	public BigDecimal countDepositMoney(Long bankId, Long areaId) {
		GwjFinanceMoney financeMoney = new GwjFinanceMoney();
		financeMoney.setBankId(bankId);
		financeMoney.setAreaId(areaId);
		return financeMoneyMapper.countDepositMoney(financeMoney);
	}

	/**
	 * 新增缴存金额收入总帐明细
	 *
	 * @param financeMoney 缴存金额收入总帐明细
	 * @return 结果
	 */
	@Override
	public int insertFinanceMoney(GwjFinanceMoney financeMoney) {
		return financeMoneyMapper.insertFinanceMoney(financeMoney);
	}

	/**
	 * 修改缴存金额收入总帐明细
	 *
	 * @param financeMoney 缴存金额收入总帐明细
	 * @return 结果
	 */
	@Override
	public int updateFinanceMoney(GwjFinanceMoney financeMoney) {
		return financeMoneyMapper.updateFinanceMoney(financeMoney);
	}

	/**
	 * 批量删除缴存金额收入总帐明细
	 *
	 * @param moneyIds 需要删除的缴存金额收入总帐明细主键
	 * @return 结果
	 */
	@Override
	public int deleteFinanceMoneyByMoneyIds(Long[] moneyIds) {
		for (Long moneyId : moneyIds) {
			GwjFinanceMoney financeMoney =financeMoneyMapper.selectFinanceMoneyByMoneyId(moneyId);
			if (StringUtils.isNotNull(financeMoney)) {
				if (financeMoney.getSelectFlag().equals(GwjConstants.YES)){
					throw new ServiceException("数据中存在已经分户处理选项，不能进行删除！");
				}
				GwjFinanceMoney temp = new GwjFinanceMoney();
				temp.setDelFlag(UserStatus.DELETED.getCode());
				temp.setMoneyId(moneyId);
				financeMoneyMapper.updateFinanceMoney(temp);
			}
		}
		return 1;
	}

	/**
	 * 删除缴存金额收入总帐明细信息
	 *
	 * @param moneyId 缴存金额收入总帐明细主键
	 * @return 结果
	 */
	@Override
	public int deleteFinanceMoneyByMoneyId(Long moneyId) {
		GwjFinanceMoney financeMoney =financeMoneyMapper.selectFinanceMoneyByMoneyId(moneyId);
		if (StringUtils.isNotNull(financeMoney) && financeMoney.getSelectFlag().equals(GwjConstants.YES)) {
			throw new ServiceException("该数据已经分户处理，不能进行删除！");
		}
		GwjFinanceMoney temp = new GwjFinanceMoney();
		temp.setMoneyId(moneyId);
		temp.setDelFlag(UserStatus.DELETED.getCode());
		return financeMoneyMapper.updateFinanceMoney(temp);
	}

	/**
	 * 缴存金额收入总帐明细清空绑定的业务信息
	 *
	 * @param financeMoney 缴存金额收入总帐明细
	 * @return 结果
	 */
	@Override
	public int clearFinanceMoneyBizInfo(GwjFinanceMoney financeMoney) {
		return financeMoneyMapper.clearFinanceMoneyBizInfo(financeMoney);
	}
}
